package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IOrderService;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OrderReviewInfoModuleVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OrderReviewVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferAttrCache;
import com.iwhalecloud.bss.kite.dataservice.entity.OfferAttr;
import com.iwhalecloud.bss.kite.manager.scene.service.QuerySceneInstService;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author yangwj
 */
@Service
public class OrderReviewInfoModuleHandler extends AbstractModuleHandler {

    @Autowired
    private QuerySceneInstService querySceneInstService;

    @Autowired
    private IOrderService orderService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        List<OrderReviewVO> goodsInfoList = this.getGoodsInfo(context, sceneModule);
        if (KiteListUtils.isEmpty(goodsInfoList)) {
            return Lists.newArrayList();
        }
        OrderReviewInfoModuleVO resourceCheckInfo = new OrderReviewInfoModuleVO();
        resourceCheckInfo.setResourceCheckList(goodsInfoList);
        ((SceneNewOfferSeneVO) context.getSceneVo()).setOrderReciveInfoModuleVO(resourceCheckInfo);
        return Collections.singletonList(resourceCheckInfo);
    }

    private List<OrderReviewVO> getGoodsInfo(AcceptContext context, SceneModule sceneModule) {
        //获取orderId
        String orderId = this.querySceneInstService
            .getExtParameterValue(context.getSceneVo().getSceneInstId(), "outOrderId");
        if (KiteStringUtils.isEmpty(orderId)) {
            return Lists.newArrayList();
        }

        Map<String, Object> dataMap = orderService.getOrderDetail(orderId);

        //        Map<String, Object> dataMap = this.bizService.getBizServiceList(orderId);

        JSONObject dataJson = JSONObject.parseObject(JSONObject.toJSONString(dataMap));

        if (KiteObjectUtils.isEmpty(dataJson)) {
            return Lists.newArrayList();
        }
        KitePosDTO kitePosDTO = JSONObject.toJavaObject(JSON.parseObject(JSON.toJSONString(dataMap)), KitePosDTO.class);
        List<OfferAttr> offerAttrs = OfferAttrCache.get(context.getAcceptRequest().getOfferId());

        List<OrderReviewVO> goodsInfoList = new ArrayList<>();
        //主商品
        OrderReviewVO mainGoodsInfo = new OrderReviewVO();
        mainGoodsInfo.setOrderNode(orderId);
        //业务号码
        if (!KiteObjectUtils.isEmpty(kitePosDTO) && !KiteObjectUtils.isEmpty(kitePosDTO.getOrder())) {
            mainGoodsInfo.setOrderNode(kitePosDTO.getOrder().getFlowNodeName());
        }
        if (!KiteObjectUtils.isEmpty(kitePosDTO) && KiteListUtils.isNotEmpty(kitePosDTO.getGoods()) && KiteListUtils
            .isNotEmpty(kitePosDTO.getUsers())) {

            mainGoodsInfo.setBizNum(kitePosDTO.getUsers().get(0).getSerialNumber());
            mainGoodsInfo.setGoodsName(kitePosDTO.getGoods().get(0).getSaleGoodsName());

            if (KiteListUtils.isNotEmpty(offerAttrs)) {
                offerAttrs.stream().forEach(offerAttr -> {
                    if ("cateName".equals(offerAttr.getFieldName())) {
                        mainGoodsInfo.setGoodsCatagory(offerAttr.getDefaultValue());
                        return;
                    }
                });
            }

        }
        goodsInfoList.add(mainGoodsInfo);
        //将数据存入SceneNewOfferSeneVO对象
        return goodsInfoList;
    }
}
